package ManHinhChinh;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.MessageBox;
import org.eclipse.swt.widgets.Monitor;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Text;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.events.ShellAdapter;
import org.eclipse.swt.events.ShellEvent;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.graphics.Rectangle;

public class ThayDoiQuyDinh {

	protected Shell TDQD;
	private Text txt_SLNTT;
	private Text txt_SLNTD;
	private Text txt_SLTDPN;
	private boolean started = false;

	/**
	 * Launch the application.
	 * @param args
	 */
	

	/**
	 * Open the window.
	 */
	public void open() {
		Display display = Display.getDefault();
		createContents();
		
		// canh giua man hinh
				Monitor primary = display.getPrimaryMonitor();
				Rectangle bounds = primary.getBounds();
				Rectangle rect = TDQD.getBounds();
				int x = bounds.x + (bounds.width - rect.width) / 2;
				int y = bounds.y + (bounds.height - rect.height) / 2;
				TDQD.setLocation(x, y);
		TDQD.open();
		TDQD.layout();
		while (!TDQD.isDisposed()) {
			if (!display.readAndDispatch()) {
				display.sleep();
			}
		}
	}

	/**
	 * Create contents of the window.
	 * @wbp.parser.entryPoint
	 */
	protected void createContents() {
		TDQD = new Shell();
		TDQD.addShellListener(new ShellAdapter() {
			@Override
			public void shellActivated(ShellEvent e) {
				if (!started)
				{
					TaoKetNoi s = new TaoKetNoi();
					Connection con = s.TaoLienKet();
					try {
						Statement stm = con.createStatement();
						ResultSet rs = stm.executeQuery("Select * from THAM_SO");
						while (rs.next()) {
							txt_SLNTT.setText(Integer.toString(rs.getInt("SOLUONGNHAPTOITHIEU")));
							txt_SLNTD.setText(Integer.toString(rs.getInt("SOLUONGNHAPTOIDA")));
							txt_SLTDPN.setText(Integer.toString(rs.getInt("SOLUONGTONDUOCNHAP")));
						}
						con.close();
					} catch (SQLException e1) {
						// TODO Auto-generated catch block
						e1.printStackTrace();
						MessageBox mb = new MessageBox (TDQD, SWT.ERROR);
						mb.setText("Database Error");
						mb.setMessage(e1.getMessage());
						mb.open();
						return;
					}
				}
			}
		});
		TDQD.setSize(304, 369);
		TDQD.setText("Thay đổi quy định");
		
		Label lblSLngNhp = new Label(TDQD, SWT.NONE);
		lblSLngNhp.setBounds(10, 31, 132, 15);
		lblSLngNhp.setText("Số lượng nhập tối thiểu");
		
		Label lblSLngNhp_1 = new Label(TDQD, SWT.NONE);
		lblSLngNhp_1.setBounds(10, 61, 132, 15);
		lblSLngNhp_1.setText("Số lượng nhập đối đa");
		
		Label lblSLngTn = new Label(TDQD, SWT.NONE);
		lblSLngTn.setBounds(10, 93, 164, 15);
		lblSLngTn.setText("Số lượng tồn được phép nhập");
		
		txt_SLNTT = new Text(TDQD, SWT.BORDER);
		txt_SLNTT.setBounds(181, 25, 86, 21);
		
		txt_SLNTD = new Text(TDQD, SWT.BORDER);
		txt_SLNTD.setBounds(181, 58, 86, 21);
		
		txt_SLTDPN = new Text(TDQD, SWT.BORDER);
		txt_SLTDPN.setBounds(181, 93, 86, 21);
		
		/////////////////////////////////////////
		////////////////// button OK////////////////////////
		////////////////////////////////////////
		Button btnOk = new Button(TDQD, SWT.NONE);
		btnOk.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				if (txt_SLNTD.getText().equals("")) {
					MessageBox mb = new MessageBox(TDQD, SWT.ICON_ERROR
							| SWT.OK);
					mb.setText("Error");
					mb.setMessage("Nhập số lượng nhập tối đa.");
					mb.open();
					txt_SLNTD.forceFocus();
					return;
				}
				
				if (txt_SLNTT.getText().equals("")) {
					MessageBox mb = new MessageBox(TDQD, SWT.ICON_ERROR
							| SWT.OK);
					mb.setText("Error");
					mb.setMessage("Nhập số lượng nhập tối thiểu.");
					mb.open();
					txt_SLNTT.forceFocus();
					return;
				}
				
				if (txt_SLTDPN.getText().equals("")) {
					MessageBox mb = new MessageBox(TDQD, SWT.ICON_ERROR
							| SWT.OK);
					mb.setText("Error");
					mb.setMessage("Nhập số lượng tồn được phép nhập.");
					mb.open();
					txt_SLTDPN.forceFocus();
					return;
				}
				
				TaoKetNoi s = new TaoKetNoi();
				Connection con = s.TaoLienKet();
				try {
					PreparedStatement pstm = con.prepareStatement("Update THAM_SO set SOLUONGNHAPTOITHIEU = ?, SOLUONGNHAPTOIDA = ?, SOLUONGTONDUOCNHAP = ?");
					pstm.setInt(1, Integer.parseInt(txt_SLNTT.getText().trim()));
					pstm.setInt(2, Integer.parseInt(txt_SLNTD.getText().trim()));
					pstm.setInt(3, Integer.parseInt(txt_SLTDPN.getText().trim()));
					pstm.executeUpdate();
					con.close();
				} catch (SQLException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
					MessageBox mb = new MessageBox (TDQD, SWT.ERROR);
					mb.setText("Database Error");
					mb.setMessage(e1.getMessage());
					mb.open();
					return;
				}
				
				MessageBox mb = new MessageBox (TDQD, SWT.OK);
				mb.setText("");
				mb.setMessage("thay đổi thành công");
				mb.open();
			}
		});
		btnOk.setBounds(192, 295, 75, 25);
		btnOk.setText("Ok");

	}
}
